package com.wingsboat;

import com.wingsboat.interfaces.Filter;
import com.wingsboat.interfaces.FilterChain;
import com.wingsboat.interfaces.FilterContext;

/**
 * 过滤器链默认实现
 *
 * @author wangzhong
 */
public class DefaultFilterChain<T extends FilterContext> implements FilterChain<T> {
    private final Filter filter;

    private FilterChain next;

    public DefaultFilterChain(Filter filter, FilterChain next) {
        this.filter = filter;
        this.next = next;
    }

    /**
     * 获取过滤实体
     *
     * @return
     */
    @Override
    public Filter getFilter() {
        return filter;
    }

    /**
     * 获取下一个过滤链节点
     *
     * @return
     */
    @Override
    public FilterChain getNextFilterChain() {
        return next;
    }

    /**
     * 设置next
     *
     * @param filterChain
     * @return
     */
    @Override
    public FilterChain setNextFilterChain(FilterChain filterChain) {
        next = filterChain;
        return this;
    }

    /**
     * 过滤操作
     *
     * @param filterContext 过滤上下文
     */
    @Override
    public void handle(T filterContext) {
        filter.doFilter(filterContext,this);
    }

    /**
     * 传递
     *
     * @param filterContext 过滤上下文
     */
    @Override
    public void fireNext(T filterContext) {
        if(null != next){
            if(filterContext.isContinue()){
                next.handle(filterContext);
            }
        }
    }
}
